<?php
/**
 * Created by PhpStorm.
 * User: admin
 * Date: 2021.3.19
 * Time: 15:28
 */


interface Middleware
{
    public static function handle(Closure $closure);
}

class VerifyCsrfToken implements Middleware
{
    public static function handle(Closure $next)
    {
        echo '验证csrf Token <br>';
        $next();
    }
}

class VerifyAuth implements Middleware
{
    public static function handle(Closure $next)
    {
        echo '验证Verify Auth <br>';
        $next();
    }
}

class SetCookie implements Middleware
{
    public static function handle(Closure $next)
    {
        $next();
        echo '验证cookie cooloe <br>';
    }
}

$handle = function() {

    echo '当前要执行的程序!';
};

$pipeArr = [
    'VerifyCsrfToken',
    'VerifyAuth',
    'SetCookie'
];
$callback = array_reduce($pipeArr,function ($stack,$pipe) {
    return function () use ($stack,$pipe) {
        return $pipe::handle($stack);
    };
});

call_user_func($callback);